package cn.kmsoft.stgeometry.landm;

import cn.kmsoft.wololo.object.GeoJsonFeature;
import cn.kmsoft.wololo.object.GeoJsonGeometry;
import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Block implements Serializable {
    /*
   4,0.012,1,陈柏怀,面,H49G031010,农村宅基地,1,@
   J1,1,3403393.438,36559307.520
   J2,1,3403392.408,36559319.280
   J3,1,3403382.235,36559318.710
   J4,1,3403383.570,36559306.670
   J1,1,3403393.438,36559307.520
   */
    //界址点数，如4
    private int jzds = 0;
    //地块面积,如0.012
    private double dkmj = 0.0;
    //地块编号,如1
    private String dkbh = "";
    //地块名称,如陈柏怀
    private String dkmc = "";
    //记录图形属性,如面
    private String jltxsx = "";
    //图幅号,如H49G031010
    private String tfh = "";
    //地块用途,如农村宅基地
    private String dkyt = "";
    //地类编码,如1
    private String dlbh = "";

    //地块的ring环的集合
    private List<Ring> rings;
    //地块属性集合
    private Map<String, Object> bAttrs = null;


    public int getJzds() {
        return jzds;
    }

    public void setJzds(int jzds) {
        this.jzds = jzds;
    }

    public double getDkmj() {
        return dkmj;
    }

    public void setDkmj(double dkmj) {
        this.dkmj = dkmj;
    }

    public String getDkbh() {
        return dkbh;
    }

    public void setDkbh(String dkbh) {
        this.dkbh = dkbh;
    }

    public String getDkmc() {
        return dkmc;
    }

    public void setDkmc(String dkmc) {
        this.dkmc = dkmc;
    }

    public String getJltxsx() {
        return jltxsx;
    }

    public void setJltxsx(String jltxsx) {
        this.jltxsx = jltxsx;
    }

    public String getTfh() {
        return tfh;
    }

    public void setTfh(String tfh) {
        this.tfh = tfh;
    }

    public String getDkyt() {
        return dkyt;
    }

    public void setDkyt(String dkyt) {
        this.dkyt = dkyt;
    }

    public String getDlbh() {
        return dlbh;
    }

    public void setDlbh(String dlbh) {
        this.dlbh = dlbh;
    }


    //构造函数
    public Block() {
        this.rings = new ArrayList<Ring>();
        this.bAttrs = new HashMap<String, Object>();
    }

    //属性
    public List<Ring> getRings() {
        return rings;
    }

    //所有属性集合
    public Map<String, Object> getbAttrs() {
        return bAttrs;
    }

    public void setbAttrs(Map<String, Object> bAttrs) {
        this.bAttrs = bAttrs;
    }

    /**
     * 生成当前地块图形的GeoJSON串
     *
     * @return
     */
    public String toJsonStr() {
        GeoJsonFeature fea = new GeoJsonFeature();
        fea.setType("Feature");
        try {
            int ringCount = this.getRings().size();
            //获取某个地块block的界址点数
            int jzdCount = 0;
            for (int ii = 0; ii < ringCount; ii++) {
                Ring ring = this.getRings().get(ii);
                int ringJzdCount = ring.getPoints().size();

                jzdCount += ringJzdCount;
            }

            //属性信息
            Map<String, Object> properties = new HashMap<String, Object>();

            String dkmj = String.format("%.4f", this.getDkmj());
            String dkbh = this.getDkbh();
            String dkmc = this.getDkmc();
            String jltxsx = this.getJltxsx();
            String tfh = this.getTfh();
            String dkyt = this.getDkyt();
            properties.put("dkbh", dkbh);
            properties.put("dkmc", dkmc);
            properties.put("jltxsx", jltxsx);
            properties.put("tfh", tfh);
            properties.put("dkyt", dkyt);
            properties.put("dkmj", dkmj);

            fea.setProperties(properties);

            //坐标串
            GeoJsonGeometry geo = new GeoJsonGeometry();
            geo.setType("Polygon");
            double[][][] coordinates = new double[ringCount][][];
            for (int j = 0; j < ringCount; j++) {
                Ring ring = this.getRings().get(j);

                int pointCount = ring.getPoints().size();
                double[][] pointCoords = new double[pointCount][];
                for (int k = 0; k < pointCount; k++) {
                    Point pt = ring.getPoints().get(k);
                    double x = Double.parseDouble(String.format("%.3f", pt.getX()));
                    double y = Double.parseDouble(String.format("%.3f", pt.getY()));

                    double[] coords = new double[2];
                    coords[0] = x;
                    coords[1] = y;

                    pointCoords[k] = coords;
                }
                coordinates[j] = pointCoords;
            }

            geo.setCoordinates(coordinates);

            fea.setGeometry(geo);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return JSONObject.toJSONString(fea);
    }

}
